package app.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;

import app.utils.DBDataGetterJDBCSiotwx;

@Service("VoiceAlertService")
public class VoiceAlertService {
	//获取当前用户的警报音状态
	public String getUserVoiceAlert(Integer user){
		String sql="SELECT * FROM voice_alert WHERE belong_to="+user;
		List<HashMap<String,String>> executeResult = DBDataGetterJDBCSiotwx.executeSqlWithResult(sql);
		if (executeResult!=null&&executeResult.size()>0) {
			HashMap<String, String> entity = executeResult.get(0);
			String string = entity.get("is_alert");
			return string;
		}
		return "1";
	}
	
	//改变当前用户设置的警报音状态  UPDATE `siotwx`.`voice_alert` SET `id`=NULL, `is_alert`=NULL, `belong_to`=NULL, `gmt_create`=NULL, `gmt_modified`=NULL WHERE (`id`=0);
	public int changVoiceALert(Integer user){
		String sql="SELECT * FROM voice_alert WHERE belong_to="+user;
		List<HashMap<String,String>> executeResult = DBDataGetterJDBCSiotwx.executeSqlWithResult(sql);
		if (executeResult!=null&&executeResult.size()>0) {
			HashMap<String,String> entity = executeResult.get(0);
			String is_alert=entity.get("is_alert");
			if ("0".equals(is_alert)) {
				is_alert="1";
			}else {
				is_alert="0";
			}
			String gmt_create=entity.get("gmt_create");
			sql="UPDATE voice_alert SET `is_alert`="+is_alert+", `gmt_modified`="+System.currentTimeMillis()+" WHERE (`belong_to`="+user+")";
			int executeSqlWithoutResult = DBDataGetterJDBCSiotwx.executeSqlWithoutResult(sql);
			return executeSqlWithoutResult;
		}else {
			sql="INSERT INTO "
					+ "voice_alert (`is_alert`, `belong_to`, `gmt_create`, `gmt_modified`)"
					+ " VALUES ("+0+", "+user+", "+System.currentTimeMillis()+", "+System.currentTimeMillis()+")";
			int executeSqlWithoutResult = DBDataGetterJDBCSiotwx.executeSqlWithoutResult(sql);
			return executeSqlWithoutResult;
		}
	}
}
